package cn.yh.shop.util;

import java.util.Date;
import java.util.List;

import cn.yh.shop.product.model.Cart;
import cn.yh.shop.product.model.CartItem;
import cn.yh.shop.user.model.UserOrder;
import cn.yh.shop.user.model.UserOrderItem;

/**
 * @Description:购物车对象转化为订单对象
 * @author:李战召
 * @time:2016年12月22日 下午9:13:57
 */
public class Cart2UserOrder {
	
	public UserOrder toUserOrder(Cart cart){
		UserOrder userOrder = new UserOrder(); 
		String orderid = new OrderIdUtils().getOrderId(new Date(), 12); //生成订单id
		userOrder.setId(orderid); 
		userOrder.setUserId(cart.getUserid());  //设置用户id
		double free = (cart.getIsFree() == null) ? 0.0 : 10.0; //邮费
		userOrder.setExpressFee(free); //设置邮费
		userOrder.setTotalPrice(cart.getTotalPay()+free); //订单总价
		
		List<CartItem> cartitems = cart.getItems();
		for (CartItem cartItem : cartitems) {
			UserOrderItem orderItem = new UserOrderItem();  //订单详情
			orderItem.setOrderId(orderid); //把订单的id赋给订单详情
			orderItem.setGoodsId(cartItem.getGoodsId());  
			orderItem.setGoodsCount(cartItem.getCount());
			orderItem.setGoodsCount(cartItem.getCount());
			orderItem.setGoodsName(cartItem.getName());
			orderItem.setGoodsPrice(cartItem.getPrice()); 
			orderItem.setPakage(cartItem.getPakage());
			orderItem.setTaste(cartItem.getTaste());
			orderItem.setImgname(cartItem.getImgname());
			userOrder.getOrderItems().add(orderItem);  //把订单项加入到订单中
		}
		return userOrder;
	}
}
